package com.shadowlight.hero_story;

import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description 广播器，工具类
 * @Author cz
 * @Date 2022/3/2
 */
@Slf4j
public final class Broadcaster {

    private Broadcaster() {
    }

    //客户端信道数组
    private static final ChannelGroup CHANNEL_GROUP = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    public static void addChannel(Channel ch) {
        if (ch == null) return;
        CHANNEL_GROUP.add(ch);
    }

    public static void removeChannel(Channel ch) {
        if (ch == null) return;
        CHANNEL_GROUP.remove(ch);
    }

    public static void broadcast(Object msg) {
        if (msg == null) return;
        CHANNEL_GROUP.writeAndFlush(msg);
    }
}
